package com.bamboo.tool.template.yapi.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.bamboo.tool.template.yapi.entity.*;
import com.bamboo.tool.util.RequestUtil;

import java.net.HttpCookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YapiRequestUtil {

    public static HttpResponse login(YapiSetting setting) {
        String url = RequestUtil.getUrl(setting.getUrl(), "api/user/login");
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("email", setting.getUserName());
        paramMap.put("password", setting.getPassword());
        HttpRequest post = HttpUtil.createPost(url);
        post.body(JSONUtil.toJsonStr(paramMap));
        HttpResponse login = post.execute();
        if (login.isOk()) {
            YapiRespBase yapiRespBase = JSONObject.parseObject(login.body(), YapiRespBase.class);
            if (Integer.valueOf(0).equals(yapiRespBase.getErrorCode())) {
                String yapiToken = login.getCookieValue("_yapi_token");
                JSONObject data = (JSONObject) yapiRespBase.getData();
                int uid = data.getIntValue("uid");
                setting.setToken(yapiToken);
                setting.setUid(uid);
            } else {
                throw new RuntimeException(yapiRespBase.getErrorMsg());
            }
        } else {
            throw new RuntimeException(login.toString());
        }
        return post.execute();
    }

    public static List<YapiGroup> getGroups(YapiSetting setting) {
        String url = RequestUtil.getUrl(setting.getUrl(), "api/group/list");
        HttpRequest get = HttpUtil.createGet(url);
        get.cookie("_yapi_token=" + setting.getToken() + ";_yapi_uid=" + setting.getUid());
        YapiRespBase<List<YapiGroup>> resp = getResp(get);
        List<YapiGroup> groups = JSONObject.parseArray(JSONObject.toJSONString(resp.getData()), YapiGroup.class);
        resp.setData(groups);

        return resp.getData();
    }

    public static List<YapiProject> getProjects(YapiSetting setting, Integer groupId) {
        String url = RequestUtil.getUrl(setting.getUrl(), "api/project/list?page=1&limit=1000&group_id=" + groupId);
        HttpRequest get = HttpUtil.createGet(url);

        get.cookie("_yapi_token=" + setting.getToken() + ";_yapi_uid=" + setting.getUid());
        YapiRespBase<List<YapiProject>> resp = getResp(get);
        List<YapiProject> projects = JSONObject.parseArray(((JSONObject) resp.getData()).getString("list"), YapiProject.class);
        resp.setData(projects);
        return resp.getData();
    }

    public static String getToken(YapiSetting setting, Integer projectId) {
        String url = RequestUtil.getUrl(setting.getUrl(), "api/project/token?project_id=" + projectId);
        HttpRequest get = HttpUtil.createGet(url);
        get.cookie("_yapi_token=" + setting.getToken() + ";_yapi_uid=" + setting.getUid());
        YapiRespBase<String> resp = getResp(get);
        String token = JSONObject.toJSONString(resp.getData());
        return token;
    }

    public static List<YapiMenu> getProjectMenus(YapiSetting setting, String token, Integer projectId) {
        String url = RequestUtil.getUrl(setting.getUrl(), "api/interface/list_menu?project_id=" + projectId + "&token=" + token);
        HttpRequest get = HttpUtil.createGet(url);
        YapiRespBase<List<YapiMenu>> resp = getResp(get);
        List<YapiMenu> menus = JSONObject.parseArray(JSONObject.toJSONString(resp.getData()), YapiMenu.class);
        return menus;
    }

    public static YapiRespBase getResp(HttpRequest request) {
        HttpResponse login = request.execute();
        if (login.isOk()) {
            YapiRespBase yapiRespBase = JSONObject.parseObject(login.body(), YapiRespBase.class);
            if (Integer.valueOf(0).equals(yapiRespBase.getErrorCode())) {
                return yapiRespBase;
            } else {
                throw new RuntimeException(yapiRespBase.getErrorMsg());
            }
        } else {
            throw new RuntimeException(login.toString());
        }
    }
}
